package com.lhd.controller;

import com.lhd.entity.dto.PostActivityDto;
import com.lhd.entity.dto.UserDto;
import com.lhd.entity.enums.RoleEnum;
import com.lhd.entity.message.InformMessage;
import com.lhd.entity.po.Course;
import com.lhd.entity.po.SysClass;
import com.lhd.entity.po.User;
import com.lhd.entity.po.UserSysClass;
import com.lhd.entity.query.UserQuery;
import com.lhd.entity.query.UserSysClassQuery;
import com.lhd.entity.vo.ResponseVO;
import com.lhd.mappers.UserSysClassMapper;
import com.lhd.service.CourseService;
import com.lhd.service.UserService;
import com.lhd.service.UserSysClassService;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: liuhd
 * @Date: 2025/3/18 15:09
 * @Description: WebSocket控制层
 */

@Controller
public class MessageController extends ABaseController{

    @Resource
    private UserService userService;
    @Resource
    private CourseService courseService;
    @Resource
    private UserSysClassService userSysClassService;


    /**
     * @description:
     * @param postActivityDto
     * @param type 签到类型
     * @param uid 学生id
     * @return com.lhd.entity.vo.ResponseVO
     * @author liuhd
     * 2025/3/18 17:19
     */
    // 从哪里发过来的 类似于RequestMapper
    @MessageMapping("/send/postActivity/{type}/{uid}")
    // 发往的主题
    @SendTo("/topic/infoMessage")
    public ResponseVO sendInformMessage(PostActivityDto postActivityDto, @DestinationVariable String type,@DestinationVariable Integer uid) {
        UserDto userInfoToken = getUserInfoToken();

        InformMessage informMessage = new InformMessage();
        informMessage.setTeacherId(userInfoToken.getId());
        User teacher = userService.getUserById(userInfoToken.getId());
        informMessage.setTeacherName(teacher.getUsername());

        informMessage.setCourseId(postActivityDto.getCid());
        Course course = courseService.getCourseById(postActivityDto.getCid());
        informMessage.setCourseName(course.getName());

        informMessage.setTitle(postActivityDto.getTitle());

        informMessage.setType(type);

        informMessage.setPostTime(new Date());

        // 应该是每个学生所在的班级信息
        UserSysClassQuery userSysClassQuery = new UserSysClassQuery();
        userSysClassQuery.setUid(uid);
        List<SysClass> sysClassList = userSysClassService.findClassByQuery(userSysClassQuery);

        informMessage.setSysClassList(sysClassList);

        return getSuccessResponseVO(informMessage);
    }

}
